package com.anyonavinfo.cpaddemo.data.appinfo.source.local;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;


import com.anyonavinfo.cpaddemo.Application;
import com.anyonavinfo.cpaddemo.R;
import com.anyonavinfo.cpaddemo.data.appinfo.AppInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZuiC on 2017/1/18.
 */

public class AppInfoLocalDataSource {

    private static final String TAG = "AppInfoManager";
    private Context mContext;
    private static AppInfoLocalDataSource INSTANCE = null;


    public static AppInfoLocalDataSource getInstance(){
        if (INSTANCE == null) {
            INSTANCE = new AppInfoLocalDataSource();
        }
        return INSTANCE;
    }

    private AppInfoLocalDataSource(){
        this.mContext = Application.getContextObject();
    }

/*    public void launchApplication(String packageName){
        if (packageName != null && isAppInstalled(packageName)){
            PackageManager packageManager = mContext.getPackageManager();
            Intent appIntent = packageManager.getLaunchIntentForPackage(packageName);
            mContext.startActivity(appIntent);
        }else {
            Toast.makeText(mContext, "该应用暂不可用", Toast.LENGTH_SHORT).show();
        }
    }*/

    public List<AppInfo> getAppInfo(){
        List<AppInfo> mAllAppInfo = new ArrayList<>();
        mAllAppInfo.add(new AppInfo("玩转E100", "com.anyonavinfo.videoplay", getAppVersionCode("com.anyonavinfo.videoplay")
                , getAppVersionName("com.anyonavinfo.videoplay"), R.drawable.icon_app_e100, 0, false));
        mAllAppInfo.add(new AppInfo("搜狐视频HD", "com.sohu.tv", getAppVersionCode("com.sohu.tv")
                , getAppVersionName("com.sohu.tv"), R.drawable.icon_app_sohu, 0, false));
        mAllAppInfo.add(new AppInfo("QQ HD", "com.tencent.minihd.qq", getAppVersionCode("com.tencent.minihd.qq")
                , getAppVersionName("com.tencent.minihd.qq"), R.drawable.icon_app_qq, 0, false));
        mAllAppInfo.add(new AppInfo("大众点评", "com.wedrive.wecar.dianping.map", getAppVersionCode("com.wedrive.wecar.dianping.map")
                , getAppVersionName("com.wedrive.wecar.dianping.map"), R.drawable.icon_app_dianping, 0, false));
        mAllAppInfo.add(new AppInfo("乐播投屏", "com.hpplay.happyplay.aw", getAppVersionCode("com.hpplay.happyplay.aw")
                , getAppVersionName("com.hpplay.happyplay.aw"), R.drawable.icon_app_touping, 0, false));
        mAllAppInfo.add(new AppInfo("喜马拉雅FM", "com.ximalaya.ting.android.car", getAppVersionCode("com.ximalaya.ting.android.car")
                , getAppVersionName("com.ximalaya.ting.android.car"), R.drawable.icon_app_ximalaya, 0, false));
        mAllAppInfo.add(new AppInfo("QQ音乐HD", "com.tencent.qqmusicpad", getAppVersionCode("com.tencent.qqmusicpad")
                , getAppVersionName("com.tencent.qqmusicpad"), R.drawable.icon_app_qqmusic, 0, false));

        for (int i = 0; i < mAllAppInfo.size(); i++){
            String packageName = mAllAppInfo.get(i).getAppPackageName();
            mAllAppInfo.get(i).setIsInstalled(isAppInstalled(packageName));
            Log.e(TAG, isAppInstalled(packageName) + " " + mAllAppInfo.get(i).getAppPackageName());
        }
        return mAllAppInfo;
    }

    private String getAppVersionName(String packageName) {
        try {
            PackageManager manager = mContext.getPackageManager();
            PackageInfo info = manager.getPackageInfo(packageName, 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "找不到版本号";
        }
    }

    private String getAppVersionCode(String packageName) {
        try {
            PackageManager manager = mContext.getPackageManager();
            PackageInfo info = manager.getPackageInfo(packageName, 0);
            String version = info.versionCode + "";
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "找不到版本号";
        }
    }

    private boolean isAppInstalled(String packageName) {
        PackageManager pm = mContext.getPackageManager();
        boolean installed = false;
        try {
            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            installed = true;
        } catch (PackageManager.NameNotFoundException e) {
            installed = false;
        }
        return installed;
    }
}
